package com.jubaozan.service.platform.controller;

import com.jubaozan.c3.framework.constants.Constants;
import com.jubaozan.c3.framework.utils.ObjectUtil;
import com.jubaozan.c3.framework.utils.ResultHolder;
import com.jubaozan.c3.framework.utils.ResultStatus;
import com.jubaozan.dao.youme.domain.Activity;
import com.jubaozan.dao.youme.domain.StoreGoods;
import com.jubaozan.service.platform.decorator.PlatformActivityDecorator;
import com.jubaozan.service.platform.decorator.PlatformAuditDecorator;
import com.jubaozan.service.platform.exception.PlatformException;
import com.jubaozan.service.platform.vo.ActivityInfoVO;
import com.jubaozan.service.platform.vo.PlatformActivityVO;
import com.jubaozan.service.platform.vo.StoreGoodsVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @ClassName PlatformActivityController
 * @Description 个体平台活动相关接口
 * @Author TanRuixiang
 * @Date 2020/9/22 17:58
 * @Version 1.0
 **/
@Api(tags = {"个体平台活动相关接口"}, value = "个体平台活动相关接口")
@RestController
@RequestMapping("/activity")
@Validated
@Slf4j
public class PlatformActivityController {

    private final PlatformActivityDecorator platformActivityDecorator;

    public PlatformActivityController(PlatformActivityDecorator platformActivityDecorator){
        this.platformActivityDecorator = platformActivityDecorator;
    }

    @ApiOperation(value = "按月(months)查询活动列表和活动信息")
    @GetMapping("/{storeId}")
    public ResultHolder<List<Activity>> getListByStoreId(@ApiParam(value = "storeId", example = "0")
                                                         @PathVariable("storeId") Long storeId, Integer months) {
        return ResultHolder.success(platformActivityDecorator.getActivitysByStoreId(storeId, months));
    }

    @ApiOperation(value = "按天(days)查询活动列表和活动信息")
    @GetMapping("/activitys/{storeId}")
    public ResultHolder<List<PlatformActivityVO>> getListByDay(@RequestHeader(Constants.KEY_X_C3_AGENTID) Long userId,@ApiParam(value = "storeId", example = "0")
                                                               @PathVariable("storeId") Long storeId,Integer days) {
        return ResultHolder.success(platformActivityDecorator.getListByDay(storeId, days,userId));
    }

    @ApiOperation(value = "获取个体平台下的商品列表")
    @GetMapping("/goods/{storeId}")
    public ResultHolder<List<StoreGoodsVO>> getGoodsListById(@RequestHeader(Constants.KEY_X_C3_AGENTID) Long userId,@ApiParam(value = "storeId", example = "0")
                                                             @PathVariable("storeId") Long storeId) {
        return ResultHolder.success(platformActivityDecorator.getGoodsListById(storeId,userId));
    }
    /*@ApiOperation(value = "删除活动")
    @DeleteMapping("/{activityId}")
    public ResultHolder<Object> delete(){
        return ResultHolder.success(null);
    }*/


    @ApiOperation(value = "新建活动")
    @PostMapping("")
    public ResultHolder<Boolean> add(@RequestHeader(Constants.KEY_X_C3_AGENTID) Long userId,@Valid @RequestBody ActivityInfoVO activityInfoVO) {
        activityInfoVO.setUserId(userId);
        checkActivity(activityInfoVO);
        platformActivityDecorator.add(activityInfoVO);
        return ResultHolder.success(true);
    }


    private void checkActivity(ActivityInfoVO activityInfoVO) {
        if (ObjectUtil.isEmpty(activityInfoVO.getStoreId())) {
            throw new PlatformException("平台ID不能为空");
        }
        if(ObjectUtil.isEmpty(activityInfoVO.getName())){
            throw new PlatformException("活动名称不能为空");
        }
        if (CollectionUtils.isEmpty(activityInfoVO.getGoodsIds())) {
            throw new PlatformException("创建活动必须添加商品");
        }
        for (Long goodsId:activityInfoVO.getGoodsIds()) {
            if(goodsId<= 0L){
                throw new PlatformException("添加的商品无效");
            }
        }
        if (ObjectUtil.isEmpty(activityInfoVO.getStartTime()) || ObjectUtil.isEmpty(activityInfoVO.getEndTime())) {
            throw new PlatformException("开始时间和结束时间不能为空");
        }
        if (activityInfoVO.getStartTime().isBefore(LocalDateTime.now())) {
            throw new PlatformException("开始时间必须在当前时间之后");
        }
        if (activityInfoVO.getStartTime().isAfter(activityInfoVO.getEndTime())) {
            throw new PlatformException("结束时间不能在开始时间之前");
        }
        logger.info("获取到个体平台创建活动对象:{}",activityInfoVO);
        addBeforeJudge(activityInfoVO);
    }

    private void addBeforeJudge(ActivityInfoVO activityInfoVO) {
        //平台在这个时间段有且只有一个活动在进行.
        Activity activity = platformActivityDecorator.getActivityByTime(activityInfoVO.getStoreId(), activityInfoVO.getStartTime(), activityInfoVO.getEndTime());
        if (ObjectUtil.isNotEmpty(activity)) {
            throw new PlatformException("该时间段已有正在进行的活动");
        }
    }

}
